/*
 * Copyright 2006-2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.martinimix.domain.order;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Provides a shopping cart.
 * 
 * @author Scott Rossillo
 *
 */
public class Cart implements Serializable {
	
	private static final long serialVersionUID = 1L;
	
	private CartHeader cartHeader;
	private List cartItems;
	
	/**
	 * Creates a new cart.
	 */
	public Cart() { 
		cartHeader = new CartHeader();
		cartItems = new ArrayList();	
	}
	
	/**
	 * Adds the given item to this cart.
	 *
	 * @param cartItem the <code>CartItem</code> to add to this <code>Cart</code>
	 */
	public void add(CartItem cartItem) {
		
		if(cartItem == null) {
			throw new NullPointerException("Cannot add a null [CartItem] to this [Cart]");
		}
		this.cartItems.add(cartItem);
	}
	
	/**
	 * Returns the total price of the item in this cart.
	 * 
	 * <p>
	 * This method's return value will be <code>0.00</code>
	 * if this <code>Cart</code> is empty.
	 * </p>
	 * 
	 * @return a <code>BigDecimal<code> containing the total price
	 * of the <code>CartItem</code>s in this <code>Cart</code>
	 */
	public BigDecimal getTotalPrice() {
		
		BigDecimal totalPrice = new BigDecimal(0.00);
		CartItem cartItem;
		
		for(int i = 0; i < cartItems.size(); i++) {
			cartItem = (CartItem) cartItems.get(i);
			totalPrice = totalPrice.add(cartItem.getExtendedPrice());
		}
		
		return totalPrice;
	}
	
	/**
	 * Returns the number of items in this cart.
	 * 
	 * @return the number of items in this <code>Cart</code>
	 */
	public int size() {
		return cartItems.size();
	}

	/**
	 * Returns a list of items in this cart.
	 * 
	 * @return a <code>List</code> containing the <code>CartItem</code>s
	 * in this cart
	 */
	public List getCartItems() {
		return cartItems;
	}

	/**
	 * Returns this cart's header.
	 * 
	 * @return this cart's <code>CartHeader</code>
	 */
	public CartHeader getCartHeader() {
		return cartHeader;
	}

	/**
	 * Sets the cart header for this cart.
	 * 
	 * @param cartHeader the <code>CartHeader</code> for this cart
	 */
	public void setCartHeader(CartHeader cartHeader) {
		this.cartHeader = cartHeader;
	}

	/**
	 * Sets the cart items for this cart
	 * 
	 * @param cartItems a <code>List</code> of cart items beloging
	 * to this cart
	 */
	public void setCartItems(List cartItems) {
		this.cartItems = cartItems;
	}
	

}
